package com.xigua.xcar.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import android.content.Context;

public class OnlineAppHttpRequest extends Thread {
	public static final int METHOD_GET  = 0;
	public static final int METHOD_POST = 1;
	
	private HttpClient 	m_httpClient = null;
	private HttpPost	m_httpPost	 = null;
	private HttpGet 	m_httpGet	 = null;
	private String 		m_url 		 = null;
	private boolean		m_bIsComplete 	 = false;
	private int			m_connectTimeOut = 3000;
	private int 		m_socketTimeOut  = 3000;
	private int 		m_method		 = METHOD_GET;
	private List<NameValuePair> m_postParams = null;
	
	private OnlineAppHttpRequestListener m_listener = null;
	
	public OnlineAppHttpRequest(Context cnt, OnlineAppHttpRequestListener resultListener)
	{
		m_listener	= resultListener;
	}
	
	public boolean beginRequest(String URL, int method, int connectTimeOut, int socketTimeOut, List<NameValuePair> postParamList)
	{
		m_bIsComplete 	 = false;
		m_url 			 = URL;
		m_connectTimeOut = connectTimeOut > 0 ? connectTimeOut : m_connectTimeOut;
		m_socketTimeOut	 = socketTimeOut > 0 ? socketTimeOut: m_socketTimeOut;
		m_postParams	 = postParamList;
		
		m_httpClient = new DefaultHttpClient();	
		
		if(METHOD_POST == method)
		{
			m_httpPost	 = new HttpPost(URL);
			m_httpGet	 = null;
			m_method	 = METHOD_POST;
			
			if(null != m_httpPost && null != postParamList)
			{
				try{
					m_httpPost.setEntity(new 
							UrlEncodedFormEntity(m_postParams, HTTP.UTF_8));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}	
			}
		}else{
			m_httpGet	 = new HttpGet(URL);
			m_httpPost	 = null;
			m_method	 = METHOD_GET;
		}
		
		if(null != m_url && null != m_httpClient && (null != m_httpPost || null != m_httpGet))
		{
			start();
		}
		
		return false;
	}
	
	boolean endRequest()
	{
		if(false == m_bIsComplete)
		{
			super.interrupt();
			if(null != m_listener)
				m_listener.onHttpRequestError(OnlineAppHttpRequestListener.HTTP_RESULT_USER_CANCLE);
		}
		return true;
	}
	
	@Override
	public void run()
	{
		try{
			if(null != m_listener)
				m_listener.onHttpRequestStart(OnlineAppHttpRequestListener.HTTP_RESULT_OK);
			
			String httpResponse = null;
			
			if(m_method == METHOD_POST)
			{
				httpResponse = m_httpClient.execute(m_httpPost, new BasicResponseHandler());
			}else{
				httpResponse = m_httpClient.execute(m_httpGet, new BasicResponseHandler());
			}
			
			if(null != m_listener)
				m_listener.onHttpRequestComplete(OnlineAppHttpRequestListener.HTTP_RESULT_OK, 200, httpResponse);
		}catch(ClientProtocolException e) {
			e.printStackTrace();
			if(null != m_listener)
				m_listener.onHttpRequestComplete(OnlineAppHttpRequestListener.HTTP_RESULT_SERVER_ERROR, 404, null);
		} catch(IOException e) {
			e.printStackTrace();
			if(null != m_listener)
				m_listener.onHttpRequestError(OnlineAppHttpRequestListener.HTTP_RESULT_NETWORK_ERROR);
		}
		m_bIsComplete = true;
	}
}
